package com.example.demo.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Inheritance;

import org.springframework.format.annotation.DateTimeFormat;

import com.example.demo.exception.SPIPException;
import com.example.demo.util.JwtUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * @author 88377761
 *
 */
@Inheritance
public class BaseEntity {

	@Column(name = "creator")
	private String creator;

	@Column(name = "updater")
	private String updater;

	// 此注解是返回格式化的字符串
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	// 此注解是接收格式化的字符串，转为Date类型对象
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "create_dttm")
	private Date createDttm;

	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "update_dttm")
	private Date updateDttm;

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getUpdater() {
		return updater;
	}

	public void setUpdater(String updater) {
		this.updater = updater;
	}

	public Date getCreateDttm() {
		return createDttm;
	}

	public void setCreateDttm(Date createDttm) {
		this.createDttm = createDttm;
	}

	public Date getUpdateDttm() {
		return updateDttm;
	}

	public void setUpdateDttm(Date updateDttm) {
		this.updateDttm = updateDttm;
	}

	/**
	 * 设置操作人
	 * 
	 * @param userId
	 * @throws SPIPException
	 */
	public void setOperator() throws SPIPException {
		String userId = JwtUtil.getUserId();
		setCreator(userId);
		setUpdater(userId);
		setCreateDttm(new Date());
		setUpdateDttm(new Date());
	}

	/**
	 * 设置修改人
	 * 
	 * @param userId
	 * @throws SPIPException
	 */
	public void setUpdateOp() throws SPIPException {
		String userId = JwtUtil.getUserId();
		setUpdater(userId);
		setUpdateDttm(new Date());
	}
}
